import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React from "react";
import SecondaryLayout from "../../compoents/secondary-layout";
import classes from "./setUsername.module.less";
import CustomInput from "../../compoents/input";
import { useLockFn, useReactive } from "ahooks";
import classNames from "classnames";
import useServices from "../../hooks/useRequest";
import { useDispatch, useSelector } from "react-redux";
import { updateUserInfo } from "../../api/user";
import toast from "../../compoents/toast";
import { useLocalStorageSetState } from "../../hooks";
import { useTranslation } from "react-i18next";
import { LocalKeyMap } from "../../local";
/** 测试是否包含特殊字符 */ import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var checkSpecialChart = function(str) {
    // const reg = /[~|@|#|$|%|^|&|*|.|?|,|<|>]/;
    var reg = /^[a-zA-Z0-9]+$/;
    return reg.test(String(str));
};
function SetUsername() {
    var userInfo = useSelector(function(st) {
        return {
            userInfo: st.app.userInfo
        };
    }).userInfo;
    var _useLocalStorageSetState = _sliced_to_array(useLocalStorageSetState(), 2), setLocalStore = _useLocalStorageSetState[1];
    var t = useTranslation().t;
    var state = useReactive({
        name: userInfo.nickname
    });
    var dispatch = useDispatch();
    var services = useServices({
        updateUserInfo: updateUserInfo
    });
    var canClick = state.name && state.name.length >= 3 && state.name !== userInfo.nickname;
    var save = useLockFn(/*#__PURE__*/ _async_to_generator(function() {
        var res, err;
        return _ts_generator(this, function(_state) {
            switch(_state.label){
                case 0:
                    _state.trys.push([
                        0,
                        2,
                        ,
                        3
                    ]);
                    if (!canClick) return [
                        2
                    ];
                    return [
                        4,
                        services.updateUserInfo({
                            nickname: state.name
                        })
                    ];
                case 1:
                    res = _state.sent();
                    if (res.code == 200) {
                        dispatch.app.SET_USER_INFO({
                            nickname: state.name
                        });
                        setLocalStore(function(preLocalStore) {
                            return {
                                prevAccount: _object_spread_props(_object_spread({}, preLocalStore.prevAccount), {
                                    nickname: state.name
                                })
                            };
                        });
                        dispatch.app.BACK();
                        return [
                            2
                        ];
                    }
                    toast.createToast({
                        message: res.msg,
                        type: "errToast"
                    });
                    return [
                        3,
                        3
                    ];
                case 2:
                    err = _state.sent();
                    console.log(err);
                    return [
                        3,
                        3
                    ];
                case 3:
                    return [
                        2
                    ];
            }
        });
    }));
    return /*#__PURE__*/ _jsx(SecondaryLayout, {
        title: t(LocalKeyMap["setUserName.title"]),
        children: /*#__PURE__*/ _jsxs("div", {
            className: classes.set_username,
            children: [
                /*#__PURE__*/ _jsx(CustomInput, {
                    value: state.name,
                    onChange: function(val) {
                        if (val && !checkSpecialChart(val)) return;
                        state.name = val;
                    },
                    clear: true,
                    className: classes.input,
                    placeholder: t(LocalKeyMap["setUserName.placeholder"]),
                    maxLength: 15,
                    showMaxLength: true
                }),
                /*#__PURE__*/ _jsx("div", {
                    className: classes.tip,
                    children: t(LocalKeyMap["setUserName.warring"])
                }),
                /*#__PURE__*/ _jsx("div", {
                    className: classes.options,
                    children: /*#__PURE__*/ _jsx("div", {
                        className: classNames(classes.btn, canClick ? "" : classes.btn_disabled),
                        onClick: save,
                        children: /*#__PURE__*/ _jsx("span", {
                            children: t(LocalKeyMap.save)
                        })
                    })
                })
            ]
        })
    });
}
export default SetUsername;

 //# sourceMappingURL=index.js.map